/*
 * @(#) MessageService.java 2013-9-12 下午1:44:26
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.base;

import java.util.List;
import java.util.Locale;

import pnuts.lang.Context;

import com.datasweep.compatibility.client.DsMessages;
import com.datasweep.compatibility.client.Response;
import com.datasweep.compatibility.pnuts.Functions;

public class MessageService extends BaseService
{
	public MessageService(Context context)
	{
		super(context);
	}
	
	public MessageService(Functions functions)
	{
		super(functions);
	}
	
	public Response mergeMessagePack(DsMessages srcMessages, DsMessages descMessages)
	{
		List messageIds=srcMessages.getMessageIDs();
		List locals=srcMessages.getLocales();
		Locale	us=(Locale) locals.get(0);
		Locale	ch=(Locale) locals.get(1);
		for(int i=0;i<messageIds.size();i++)
		{
			String messageId = (String) messageIds.get(i);
			String messageUs=srcMessages.getValue(messageId,us);
			String messageCh = srcMessages.getValue(messageId,ch);
			descMessages.setMessage(messageId, us, messageUs);
			descMessages.setMessage(messageId, ch, messageCh);
		}
		return descMessages.save(null, null);
	}
	
	public Response copyMessage(DsMessages srcMessages)
	{
		List messageIds=srcMessages.getMessageIDs();
		List locals=srcMessages.getLocales();
		Locale	us=(Locale) locals.get(0);
		Locale	ch=(Locale) locals.get(1);
		for(int i=0;i<messageIds.size();i++)
		{
			String messageId = (String) messageIds.get(i);
			String messageUs=srcMessages.getValue(messageId,us);
			if(messageUs == null || messageUs.length()==0)
				continue;
			String messageCh = srcMessages.getValue(messageId,ch);
			for(int j=i+1;j<messageIds.size();j++)
			{
				String nextMessageId = (String) messageIds.get(j);
				String nextMessageUs=srcMessages.getValue(nextMessageId,us);
				String nextMessageCh = srcMessages.getValue(nextMessageId,ch);
				if(nextMessageUs != null && nextMessageUs.length()>0)
					continue;
				if(nextMessageCh != null && nextMessageCh.equals(messageCh))
				{
					srcMessages.removeMessageID(nextMessageId);
					srcMessages.setMessage(nextMessageId, us, messageUs);
					srcMessages.setMessage(nextMessageId, ch, nextMessageCh);
				}
			}

		}
		return srcMessages.save(null, null);
	}
	
	public Response copyEnMessage(DsMessages srcMessages)
	{
		List messageIds=srcMessages.getMessageIDs();
		List locals=srcMessages.getLocales();
		Locale	us=(Locale) locals.get(0);
		Locale	ch=(Locale) locals.get(1);
		for(int i=0;i<messageIds.size();i++)
		{
			String messageId = (String) messageIds.get(i);
			String messageUs = srcMessages.getValue(messageId,us);
			String messageCh=srcMessages.getValue(messageId,ch);
			if(messageUs == null || messageUs.length()==0)
				continue;

			for(int j=0;j<messageIds.size();j++)
			{
				String nextMessageId = (String) messageIds.get(j);
				String nextMessageUs=srcMessages.getValue(nextMessageId,us);
				String nextMessageCh = srcMessages.getValue(nextMessageId,ch);
				if(nextMessageUs != null && nextMessageUs.length()>0)
					continue;
				if(nextMessageCh != null && nextMessageCh.equals(messageCh))
				{
					srcMessages.removeMessageID(nextMessageId);
					srcMessages.setMessage(nextMessageId, us, messageUs);
					srcMessages.setMessage(nextMessageId, ch, nextMessageCh);
				}
			}

		}
		return srcMessages.save(null, null);
		
	}
}
